package com.zecan.blog.controller;

import com.zecan.blog.common.BlogResult;
import com.zecan.blog.common.status.StarGoodStatus;
import com.zecan.blog.common.status.UserStatus;
import com.zecan.blog.controller.annotation.AccessLimit;
import com.zecan.blog.dao.converter.BlogArticleConverter;
import com.zecan.blog.dto.atricle.ArticleDto;
import com.zecan.blog.dto.atricle.UserArticleDto;
import com.zecan.blog.entity.Article;
import com.zecan.blog.service.ArticleService;
import com.zecan.blog.service.StarGoodService;
import com.zecan.blog.utils.BlogResultUtils;
import com.zecan.blog.vo.article.ArticleMsgVo;
import com.zecan.blog.vo.stargood.StarArticleVo;
import org.quartz.SchedulerException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/**
 * @ClassName ArticleController
 * @Description
 * @date 2022/10/28 17:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private StarGoodService starGoodService;

    /**
     * 添加文章
     * @param articleDto
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public BlogResult addArticle(ArticleDto articleDto){
        // 添加文章
        Integer articleId = articleService.addArticle(BlogArticleConverter.INSTANCE.articleConvertEntity(articleDto));
        return BlogResultUtils.userStatus(articleId,UserStatus.USER_ADD_BLOG_SUCCESS);
    }


    /**
     * 删除用户单条文章信息 ==> status set = 1
     * @param articleId
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public BlogResult addMarkdownImage( Integer articleId) {
        articleService.delArticle(articleId);
        return BlogResultUtils.ok("删除文章成功");
    }

    /**
     * 加载主页的时候获取所有的文章并分页
     * @return 文章列表
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public BlogResult showList(Integer offset, Integer limit) throws ParseException {
        return BlogResultUtils.ok(articleService.getArticleList(offset,limit),"获取文章列表成功");
    }

    /**
     * 用户在个人中心进行修改文章信息
     * @param articleDto DTO
     * @return 修改结果
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public BlogResult updateArticle(ArticleDto articleDto) {
        articleService.updateArticle(articleDto);
        return BlogResultUtils.ok("修改文章信息成功");
    }


    /**
     * 获取用户的收藏夹信息
     * @param userArticleDto dto
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/favorite",method = RequestMethod.POST)
    public BlogResult getFavoriteArticle(UserArticleDto userArticleDto) {
        StarArticleVo  userFavorite = articleService.getUserFavorite(userArticleDto);
        return BlogResultUtils.ok(userFavorite,"获取收藏信息成功");
    }


    /**
     * 加载主页的时候获取所有的文章数量
     * @return 返回总数
     */
    @RequestMapping("/listCount")
    public BlogResult getArtCount() {
        return BlogResultUtils.ok(articleService.getArticleCount(),"获取文章总数成功");
    }

    /**
     * 给文章添加展示图片, 也在文章显示中显示大图
     * @param multipartFile 图片
     * @return 保存在oss的路径
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/image",method = RequestMethod.POST)
    public BlogResult addArticleImage(@RequestParam("articleImage") MultipartFile multipartFile) {
        String imageUrl = articleService.uploadArticleImage(multipartFile);
        return BlogResultUtils.ok(imageUrl,"上传成功");
    }

    /**
     * 给用户输入的markdown中添加图片
     * @param multipartFile 图片
     * @return 保存在oss中的路径
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/markdown/image",method = RequestMethod.POST)
    public BlogResult addMarkdownImage(@RequestParam("markdownImage") MultipartFile multipartFile) {
        String imageUrl = articleService.uploadMarkdownImage(multipartFile);
        return BlogResultUtils.ok(imageUrl,"上传成功");
    }


    /**
     * 展示博客信息
     * @param articleId 文章id
     * @return
     */
    @RequestMapping(value = "/showBlog/{id}",method = RequestMethod.GET)
    public BlogResult showArticle(@PathVariable("id") Integer articleId,Integer userId) {
        ArticleMsgVo articleVo = articleService.getArticleByArticleId(articleId,userId);
        return BlogResultUtils.ok(articleVo,"获取文章信息成功");
    }



    /**
     * 用户点赞文章
     * @param userId 用户id
     * @param articleId 文章id
     * @return
     * @throws SchedulerException
     */
    @AccessLimit(seconds = 60,maxCount = 5)
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/addGood" ,method = RequestMethod.POST)
    public BlogResult addGood(Integer userId,Integer articleId) throws SchedulerException {
        Boolean isGood = starGoodService.addGoodByUserIdAndArticleId(userId, articleId);
        if(isGood) {
            return BlogResultUtils.StarGoodStatus(StarGoodStatus.USER_GOOD_ARTICLE_SUCCESS);
        }else {
            return BlogResultUtils.StarGoodStatus(StarGoodStatus.USER_CANCEL_GOOD_ARTICLE_SUCCESS);
        }
    }

    /**
     * 用户收藏文章
     * @param userId 用户id
     * @param articleId 文章id
     * @return
     */
    @AccessLimit(seconds = 60,maxCount = 5)
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/addStar" ,method = RequestMethod.POST)
    public BlogResult addStar(Integer userId, Integer articleId) {
        Boolean isStar = starGoodService.addStarByUserIdAndArticleId(userId, articleId);
        if(isStar) {
            return BlogResultUtils.StarGoodStatus(StarGoodStatus.USER_STAR_ARTICLE_SUCCESS);
        }else {
            return BlogResultUtils.StarGoodStatus(StarGoodStatus.USER_CANCEL_STAR_ARTICLE_SUCCESS);
        }
    }

    /**
     * 用户在个人中心中取消收藏
     * @param userId
     * @param articleId
     * @return
     */
    @PreAuthorize("hasAuthority('user') OR hasAuthority('admin')" )
    @RequestMapping(value = "/unStar" ,method = RequestMethod.POST)
    public BlogResult unStar(Integer userId, Integer articleId) {
       starGoodService.unStarByUserIdAndArticleId(userId,articleId);
        return BlogResultUtils.StarGoodStatus(StarGoodStatus.USER_CANCEL_STAR_ARTICLE_SUCCESS);
    }

}
